<?php
// 抓取漫画列表页
set_time_limit(0);

include_once('lib/config.php');
include_once('lib/Db.class.php');
include_once('lib/Model.class.php');
include_once('lib/Upyun.class.php');
include_once('lib/Image.class.php');
include_once('lib/Http.class.php');
include_once('lib/Cut.class.php');
include_once('lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$queue_zlist_fetch_key = 'Queue_zlist_fetch';
$mh_list_ids = 'Running_zlist_to_queue';
$all_list_fetched = array('www.leshen.com');

$model = new Model;

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||queue_mh_zlist');

$limit = 50;
while($limit--) {
  $stop = $redis->get('LNPAN_MH_QUEUE_STOP');
  if($stop) {
    break;
  }

  $lsize = $redis->lSize($queue_zlist_fetch_key);
  if($lsize > 50) {
    break;
  }

  $list = $model->query('select * from news_zlist where page>0 order by id');
  if($list) {
    foreach ($list as $item) {
      for ($i=1; $i <= 3; $i++) {
        $url = ($i == 1) ? $item['link'] : str_replace('{page}', $i, $item['multi_link']);
        $parse_url = parse_url($url);

        $redis->rPush($queue_zlist_fetch_key, $item['type'] . '||' . $url);
      }

      $update_data = array('page'=>0);
      $model->save("news_zlist", "id='$item[id]'", $update_data);
    }

  } else {
    $update_data = array('page'=>1);
    $model->save("news_zlist", "page=0", $update_data);
  }

  sleep(2);
}

$model->close();
$redis->hDel(MH_PIDS, $pid);